<?php
/**
 * Created by PhpStorm.
 * User: yejia
 * Date: 2016/8/13
 * Time: 14:21
 */

namespace Admin\Controller;

use Think\Controller;
class ChangePasswordController extends Controller
{
    public function index(){
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;
        $password = M("admin")->where(array("id" => $id))->select();
        /*dump($password);*/
        $this->password=$password;
        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($password){
            $this->assign('list', $password);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/ChangePassword/index"));}
    }

    public function reset(){
        $password = trim(I('post.password'));
        $newpassword = trim(I('post.newpassword'));
        $okpassword = trim(I('post.okpassword'));

        $a=M("admin");
        $pwd = $a-> where(array('password'=>$password)) -> find();
        if(!$pwd){
            $this->error('原密码错误!',U("Admin/ChangePassword/index"));
        }
        elseif($newpassword==$password)
        {
            $this->error('新旧密码相同!',U("Admin/ChangePassword/index"));
        }
        elseif ($newpassword!=$okpassword){
            $this->error('两次密码不一致!',U("Admin/ChangePassword/index"));
        }
        else{
            $a->password=$newpassword;
            $a->save();
            $this->success('修改成功',U("Admin/ChangePassword/index"));
        }
    }



}